package mortal.game.control
{
    import Message.Game.*;
    import com.gengine.resource.info.*;
    import flash.display.*;
    import flash.events.*;
    import modules.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.model.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.scene.player.entity.*;
    import mortal.game.view.friends.defin.*;
    import mortal.game.view.system.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class FriendsController extends Controller
    {
        private var _friendModule:FriendsModule;
        private var _init:Boolean = false;
        private var _isReadFriend:Boolean = false;

        public function FriendsController()
        {
            this.Init();
            
        }

        private function Init() : void
        {
            
        }

        override protected function initView() : IView
        {
            if (this._friendModule == null)
            {
                this._friendModule = new FriendsModule();
                this._friendModule.x = 400;
                this._friendModule.y = 100;
                this._friendModule.addEventListener(WindowEvent.SHOW, this.showFriendHandler);
                this._friendModule.addEventListener(WindowEvent.CLOSE, this.closeFriendHandler);
            }
            return this._friendModule;
        }

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.SearchPlayerResult, this.onSearchPlayer);
            NetDispatcher.addCmdListener(ServerCommand.FriendApply, this.onFriendApply);
            Dispatcher.addEventListener(EventName.LoginGameSuccess, this.requestDataHandler);
            Dispatcher.addEventListener(EventName.FriendSearchPlayer, this.searchHandler);
            Dispatcher.addEventListener(EventName.FriendRequestAddFriend, this.requestAddFriendHandler);
            Dispatcher.addEventListener(EventName.FriendReplyAdd, this.replyHandler);
            Dispatcher.addEventListener(EventName.FriendDelete, this.deleteFriendHandler);
            Dispatcher.addEventListener(EventName.FriendMoveToBlackList, this.moveToBlackHandler);
            Dispatcher.addEventListener(EventName.FriendPersonalSet, this.personalSetHandler);
            Dispatcher.addEventListener(EventName.FriendButtonRequest, this.buttonRequestHandler);
            
        }

        private function showFriendHandler(event:WindowEvent) : void
        {
            this.updateFriendList();
            this.updateHeadInfo();
            NetDispatcher.addCmdListener(ServerCommand.EntityInfo_Update, this.updateHeadInfo);
            NetDispatcher.addCmdListener(ServerCommand.FriendListUpdate, this.updateFriendList);
            NetDispatcher.addCmdListener(ServerCommand.FriendAdd, this.onFriendAdd);
            NetDispatcher.addCmdListener(ServerCommand.FriendRemove, this.onFriendRemove);
            NetDispatcher.addCmdListener(ServerCommand.FriendOnlineStatus, this.onFriendOnlienStatus);
            NetDispatcher.addCmdListener(ServerCommand.FriendUpdate, this.onFriendUpdateStatus);
            NetDispatcher.addCmdListener(ServerCommand.FriendAttributeUpdate, this.onFriendAttributeUpdate);
            
        }

        private function closeFriendHandler(event:WindowEvent) : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.EntityInfo_Update, this.updateHeadInfo);
            NetDispatcher.removeCmdListener(ServerCommand.FriendListUpdate, this.updateFriendList);
            NetDispatcher.removeCmdListener(ServerCommand.FriendAdd, this.onFriendAdd);
            NetDispatcher.removeCmdListener(ServerCommand.FriendRemove, this.onFriendRemove);
            NetDispatcher.removeCmdListener(ServerCommand.FriendOnlineStatus, this.onFriendOnlienStatus);
            NetDispatcher.removeCmdListener(ServerCommand.FriendUpdate, this.onFriendUpdateStatus);
            NetDispatcher.removeCmdListener(ServerCommand.FriendAttributeUpdate, this.onFriendAttributeUpdate);
            
        }

        private function searchHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as String;
            GameProxy.friendsProxy.SearchPlayer(_loc_2);
            
        }

        private function requestDataHandler(event:Event) : void
        {
            if (!this._isReadFriend)
            {
                GameProxy.friendsProxy.getAllList();
                this._isReadFriend = true;
            }
            
        }

        private function requestAddFriendHandler(event:DataEvent) : void
        {
            GameProxy.friendsProxy.applyFriend(event.data["name"]);
            
        }

        private function replyHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["playerId"];
            var _loc_3:* = event.data["type"];
            var _loc_4:* = EFriendReplyResult._EFriendReplyResultAccept;
            switch(_loc_3)
            {
                case ReplyType.OK:
                {
                    _loc_4 = EFriendReplyResult._EFriendReplyResultAccept;
                    break;
                }
                case ReplyType.CANCEL:
                {
                    _loc_4 = EFriendReplyResult._EFriendReplyResultReject;
                    break;
                }
                default:
                {
                    break;
                }
            }
            GameProxy.friendsProxy.reply(_loc_2, _loc_4);
            
        }

        private function deleteFriendHandler(event:DataEvent) : void
        {
            var _loc_2:FriendInfo = null;
            if (event.data is FriendInfo)
            {
                _loc_2 = event.data as FriendInfo;
                GameProxy.friendsProxy.remove(_loc_2.recordId, _loc_2.friendFlag);
            }
            
        }

        private function moveToBlackHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as PlayerMiniInfo;
            GameProxy.friendsProxy.addToBlackList(_loc_2);
            
        }

        private function personalSetHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            cache.personalSetCache.isAutoReply = _loc_2["isAutoReply"];
            cache.personalSetCache.isNotShowOffline = _loc_2["isNotShowOffline"];
            cache.personalSetCache.isRefusedToStranger = _loc_2["isRefusedToStranger"];
            cache.personalSetCache.autoReplyText = _loc_2["autoReplyText"];
            this.updateAll();
            
        }

        private function buttonRequestHandler(event:DataEvent) : void
        {
            if (GameController.scene.selectEntity && GameController.scene.selectEntity is UserPlayer)
            {
                GameProxy.friendsProxy.applyFriend((GameController.scene.selectEntity as UserPlayer).entityInfo.entityInfo.name);
            }
            else
            {
                MsgManager.addBroadCast("请先选择您要添加的好友");
            }
            
        }

        private function updateHeadInfo(param1:Object = null) : void
        {
            var headImage:Bitmap;
            var obj:* = param1;
            var data:Object;
            data.name = Cache.instance.role.entityInfo.name;
            data.job = CareerConfig.instance.getInfoByCode(Cache.instance.role.entityInfo.career).name;
            data.level = Cache.instance.role.entityInfo.level;
            headImage = new Bitmap();
            data.headImage = headImage;
            AvatarInfo.getAvatar(Cache.instance.role.entityInfo.camp, Cache.instance.role.entityInfo.sex, AvatarType.FRIEND, function (param1:ImageInfo) : void
            {
                headImage.bitmapData = param1.bitmapData;
                
            }
            );
            (view as FriendsModule).updateHeadInfo(data);
            
        }

        private function onSearchPlayer(param1:Object) : void
        {
            var _loc_3:PlayerMiniInfo = null;
            var _loc_2:* = param1["isFind"];
            if (_loc_2)
            {
                _loc_3 = new PlayerMiniInfo(param1["player"]);
            }
            (view as FriendsModule).updateSearchResult(_loc_2, _loc_3);
            
        }

        private function onFriendApply(param1:Object) : void
        {
            var _loc_2:PlayerMiniInfo = null;
            if (param1 is SApplyMsg)
            {
                _loc_2 = new PlayerMiniInfo((param1 as SApplyMsg).fromPlayer);
                if (!SystemSetter.currentSetter.refuseFriend)
                {
                    (view as FriendsModule).showApply(_loc_2);
                }
                else
                {
                    GameProxy.friendsProxy.reply(_loc_2.playerId, EFriendReplyResult._EFriendReplyResultReject);
                }
            }
            
        }

        private function updateFriendList(param1:Object = null) : void
        {
            this.updateAll();
            this._init = true;
            
        }

        private function onFriendAdd(param1:Object) : void
        {
            if (this._init)
            {
                this.updateAll();
            }
            
        }

        private function onFriendRemove(param1:Object) : void
        {
            if (this._init)
            {
                this.updateAll();
            }
            
        }

        private function onFriendOnlienStatus(param1:Object) : void
        {
            if (this._init)
            {
                this.updateAll();
            }
            
        }

        private function onFriendUpdateStatus(param1:Object) : void
        {
            if (this._init)
            {
                this.updateAll();
            }
            
        }

        private function onFriendAttributeUpdate(param1:Object) : void
        {
            if (this._init)
            {
                this.updateAll();
            }
            
        }

        private function updateAll() : void
        {
            (view as FriendsModule).updateFriendList(cache.friend.getFriendList(), cache.personalSetCache.isNotShowOffline);
            (view as FriendsModule).updateEnemyList(cache.friend.getEnemyList(), cache.personalSetCache.isNotShowOffline);
            (view as FriendsModule).updateBlackList(cache.friend.getBlackList(), cache.personalSetCache.isNotShowOffline);
            (view as FriendsModule).updateRecentList(cache.friend.getRecentList(), cache.personalSetCache.isNotShowOffline);
            
        }

    }
}
